/**
 * 提供微信登录、注册、绑定相关的方法
 */



/**
 * 微信登录
 * @param  {[type]} info [用户信息]
 * @param  {[type]} successCallback [成功回调]
 * @param  {[type]} errorCallback [错误回调]
 */
function wxLogin(info, successCallback, errorCallback) {
  $.ajax({
    url: 'ajax/wxlogin',
    type: 'post',
    async: true,
    data: {
      openId: info.openid,
      unionId: info.unionid
    },
    dataType: 'json',
    success: function(data) {
      if (data.result != null) {
        console.log('ajax:登录成功');
        successCallback(data.result);
      }
      else {
        console.log('ajax:登录失败');
        errorCallback();
      }
    }
  });
}

/**
 * 微信注册
 * @param  {[type]} info [用户信息]
 * @param  {[type]} successCallback [成功回调]
 * @param  {[type]} errorCallback [错误回调]
 */
function wxRegister(info, successCallback, errorCallback) {

  $.ajax({
    url: 'ajax/wxregister',
    type: 'post',
    async: true,
    data: {
      nickName: info.nickname,
      avatar: info.headimgurl,
      sex: info.sex,
      openId: info.openid,
      unionId: info.unionid,
    },
    dataType: 'json',
    success: function(data) {
      if (data.returnCode == 0) {
        console.log('ajax:注册成功');
        successCallback(data.result);
      }
      else {
        console.log('ajax:注册失败');
        console.log(data.message);
        errorCallback();
      }
    }
  });
}

/**
 * 微信注册，有邮箱
 * @param  {[type]} info [用户信息]
 * @param  {[type]} successCallback [成功回调]
 * @param  {[type]} errorCallback [错误回调]
 */
function wxBind(info, emailInfo, successCallback, errorCallback) {
  $.ajax({
    url: 'ajax/bind',
    type: 'post',
    async: true,
    data: {
      nickName: info.nickname,
      avatar: info.headimgurl,
      sex: info.sex,
      unionId: info.unionid,
      email: emailInfo.email,
      password: emailInfo.password,
    },
    dataType: 'json',
    success: function(data) {
      if (data.returnCode == 0) {
        console.log('ajax:绑定成功');
        successCallback(data.result);
      }

      else {
        alert('绑定失败');
        console.log('ajax:绑定失败');
        console.log(data.message);
        errorCallback();
      }
    }
  });
}


/**
 * 三方注册
 * @param  {[type]} info [用户信息]
 * @param  {[type]} successCallback [成功回调]
 * @param  {[type]} errorCallback [错误回调]
 */
function wxregister2(info, emailInfo, successCallback, errorCallback) {
  var _nickName = info.nickname + Math.ceil(Math.random() * 1000);
  $.ajax({
    url: 'ajax/wxregister2',
    type: 'post',
    async: true,
    data: {
      nickName: info.nickname,
      avatar: info.headimgurl,
      sex: info.sex,
      unionId: info.unionid,
      email: emailInfo.email,
      password: emailInfo.password,
    },
    dataType: 'json',
    success: function(data) {
      if (data.returnCode == 0) {
        successCallback(data.result);
      }
      else {
        alert('邮箱注册失败');
        console.log(data.message);
        errorCallback();
      }
    }
  });
}

/**
 * 验证邮箱
 * @param  {[type]} info [用户信息]
 * @param  {[type]} successCallback [成功回调]
 * @param  {[type]} errorCallback [错误回调]
 */
function checkEmail(info, emailInfo, successCallback, errorCallback) {
  $.ajax({
    url: 'ajax/checkEmail',
    type: 'post',
    async: true,
    data: {
      email: emailInfo.email,
    },
    dataType: 'json',
    success: function(data) {
      if (data.returnCode == 0) {
        if(!data.result) {
          //邮箱未注册，直接三方注册
          wxregister2(info, emailInfo, successCallback, errorCallback);
        } else {
          //邮箱已注册，三方绑定
          wxBind(info, emailInfo, successCallback, errorCallback);
        }
      }
      else {
        console.log('ajax:验证邮箱');
        console.log(data.message);
        errorCallback();
      }
    }
  });
}
